package com.tiza.xiaoyoubao.ui.fragment;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.entity.EditInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;

import org.simple.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Request;

/**
 * Created by mapple on 17/3/19.
 * 绑定qq号
 */

public class QQBingdingFragment extends BaseFragment {

    @BindView(R.id.tv_qq)
    TextView tv_qq;
    @BindView(R.id.et_qq)
    EditText et_qq;

    private String etqq;
    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);

    @Override
    protected int getLayoutId() {
        return R.layout.frament_qq_bingding;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        if(!TextUtils.isEmpty(userInfo.getWebzzCmsBuyerInfo().getQqCard())){
            tv_qq.setText(userInfo.getWebzzCmsBuyerInfo().getQqCard());
        }else{
            tv_qq.setText("未绑定");
        }

        intWatch();
    }


    @OnClick({R.id.bt_save})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.bt_save:
                etqq=et_qq.getText().toString();
                if(TextUtils.isEmpty(etqq)){
                    toast("你输入的qq号为空");
                    return;
                }else{
                    setqqCard();
                }
                break;
            default:
                break;
        }
    }
    private void setqqCard() {
        ApiHttpClient.setUserInfo(QQBingdingFragment.this, userInfo.getWebzzCmsBuyerInfo().getTelphone(), etqq,"3",
                                     new JsonCallback<String>(String.class, true) {

                                         @Override
                                         public void onBefore(Request request) {
                                             super.onBefore(request);
                                         }

                                         @Override
                                         public void onError(Call call, Exception e) {
                                             toast("保存失败");
                                         }

                                         @Override
                                         public void onResponse(ResponseInfo<String> response) {
                                             int code = response.getCode();
                                             String message = response.getMessage();
                                             if (code == 1) {
                                                 showWarnDialog(message);
                                                 return;
                                             }
                                             toast("QQ修改成功!");
                                             userInfo.getWebzzCmsBuyerInfo().setQqCard(etqq);
                                             Hawk.put(Constants.KEY_USER_INFO, userInfo);
                                             EventBus.getDefault().post(new EditInfo(EditInfo
                                                                                             .TYPE_GET_QQ_NAME, etqq),
                                                                        Constants.TAG_SET_USERINFO_SUCCESS
                                             );
                                             finish();
                                         }

                                         @Override
                                         public void onAfter() {
                                             super.onAfter();
                                         }
                                     }
        );
    }

    public void intWatch() {
        et_qq.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                et_qq.setTextColor(getResources().getColor(R.color.textColorThird));
            }
        });

    }
}
